package com.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class CIBNUtil {

    public static final String PLAY_URL = "http://api.epg2.cibn.cc/getPlayUrl";

    public static void main(String[] args) {
        String url = getPlayerUrl("258359","0",0);
        System.out.println(url);
    }


    public static String getPlayerUrl(String contentId, String childId, int type) {
        Map hashMap = new HashMap();
        hashMap.put("epgId", "1093");
        hashMap.put("contentId", contentId);
        hashMap.put("childId", childId);
        if (type == 0) {
            hashMap.put("contentType", "video");
        } else if (type == 1) {
            hashMap.put("contentType", "live");
        } else if (type == 2) {
            hashMap.put("contentType", "loop");
        }
        hashMap.put("projectId", "107"); // "107"
        hashMap.put("appId", "1018"); // "1018"
        hashMap.put("channelId", "20155"); // "20155"
        hashMap.put("userId", "12629009"); // "1532592333179075"
        hashMap.put("termId", "7962784");
        hashMap.put("version", "1.0.0.0");
        String toJSONString = JSONObject.toJSONString(hashMap);
        try {
            String response = HttpUtil.post(PLAY_URL, toJSONString);
            System.out.println(response);
            JSONObject jsonObject = (JSONObject) JSONObject.parse(response);
            JSONObject jsonObject2 = (JSONObject) ((JSONArray) ((JSONObject) jsonObject.get("data")).get("media")).get(0);
            return jsonObject2.getString("url");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
